package main

import (
	"fmt"
	"reflect"
	"runtime"
)

func compute(a, b int, op string) (int, error) {
	switch op {
	case "+":
		return a + b, nil
	case "-":
		return a - b, nil

	case "*":
		return a * b, nil

	case "/":
		q, _ := div(a, b)

		return q, nil
		//return a / b
	default:
		return 0, fmt.Errorf("unsupport operation: %s", op)
		//panic("unsupport operation:"+ op)
	}
}
func div(a, b int) (q, r int) {
	return a / b, a % b

}

func apply(op func(int, int) int, a, b int) int {
	p := reflect.ValueOf(op).Pointer()
	opName := runtime.FuncForPC(p).Name()
	fmt.Printf("Calling function  %s with args"+
		"(%d,%d)", opName, a, b)
	return op(a, b)
}

func sum(numbers ...int)  int{
	s := 0
	for i := range numbers {
		s += numbers[i]
	}
	return s
}

//func swap(a,b *int)  {
//	*b,*a = *a,*b
//}
func swap(a,b int) (int,int) {
	return  b,a
}

func main() {
	//if a, err := compute(1, 1, "**"); err != nil {
	//	fmt.Println("Error:", err)
	//} else {
	//	fmt.Println(a)
	//}

	//apply((a1,a2)->a1+a2,1,2)

	//
	//fmt.Println(apply(
	//	func(i1 int,i2 int) int {
	//		return int(math.Pow(float64(i1),float64(i2)))
	//	},1,2))

	//可变参数
	//fmt.Println(sum(1,2,3,4,5,6))

	//fmt.Println(compute(1,1,"+"))
	//fmt.Println(compute(2,1,"/"))
	//q	,	r :=div(13,3)
	//fmt.Println(div(13,3))
	//fmt.Println(q,r)

	a := 3
	b := 4
	//swap(&a,&b)
	fmt.Println(a,b)

	fmt.Println(swap(a,b))
}
